/*
 * MainWindow.java
 * 
 * Created on Aug 18, 2007, 6:44:08 PM
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.usixml.model.client.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import org.usixml.model.client.OpenListener;

/**
 *
 * @author htmfilho
 */
public class MainWindow extends JFrame implements ActionListener {
    
    private MenuBar menuBar;
    private JSplitPane splitExternalResources;
    private JSplitPane splitInternalResources;
    private JTree treProcessStructure;
    private JPanel pnlProcessStructure;
    private JPanel pnlProcStructCommands;
    private TaskModelPanel pnlTaskModel;
    private UserInterfacePanel pnlUserInterface;
    private JScrollPane scpProcessStructure;
    private JButton btCollapse;
    private JButton btExpand;
    
    private static final int widthProcessStructure = 250;
    private static final int widthUserInterface = 250;
    
    public MainWindow() {
        super("UsiXML");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        initComponent();
    }
    
    private void initComponent() {
        menuBar = new MenuBar();
        menuBar.setOpenListener(new OpenListener(this));
        this.setJMenuBar(menuBar);
        
        splitExternalResources = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitExternalResources.setOneTouchExpandable(true);
        splitExternalResources.setDividerLocation(widthProcessStructure);
        
        pnlProcessStructure = new JPanel(new BorderLayout());
        
        JPanel pnlTitle = new JPanel(new FlowLayout(FlowLayout.LEFT));
        pnlTitle.add(new JLabel("<html><b>Process Structure</b></html>"));
        pnlTitle.setBackground(Color.LIGHT_GRAY);
        pnlProcessStructure.add(pnlTitle, BorderLayout.NORTH);
        
        pnlProcStructCommands = new JPanel(new FlowLayout(FlowLayout.LEFT));
        btExpand = new JButton("Expand");
        btExpand.addActionListener(this);
        pnlProcStructCommands.add(btExpand);
        btCollapse = new JButton("Collapse");
        btCollapse.addActionListener(this);
        pnlProcStructCommands.add(btCollapse);
        pnlProcStructCommands.setVisible(false);
        pnlProcessStructure.add(pnlProcStructCommands, BorderLayout.SOUTH);
        
        splitExternalResources.setLeftComponent(pnlProcessStructure);
        
        splitInternalResources = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitInternalResources.setOneTouchExpandable(true);
        
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        splitInternalResources.setDividerLocation((int)d.getWidth() - widthProcessStructure - widthUserInterface);
        
        pnlTaskModel = new TaskModelPanel();
        splitInternalResources.setLeftComponent(pnlTaskModel);
        
        pnlUserInterface = new UserInterfacePanel();
        splitInternalResources.setRightComponent(pnlUserInterface);
        
        splitExternalResources.setRightComponent(splitInternalResources);
        
        this.getContentPane().add(splitExternalResources, BorderLayout.CENTER);
    }
    
    public void setTreProcessStructure(JTree treProcessStructure) {
        this.treProcessStructure = treProcessStructure;
        
        ToolTipManager.sharedInstance().registerComponent(this.treProcessStructure);
        
        if(scpProcessStructure != null)
            pnlProcessStructure.remove(scpProcessStructure);
        scpProcessStructure = new JScrollPane(this.treProcessStructure);
        
        pnlProcessStructure.add(scpProcessStructure, BorderLayout.CENTER);
        pnlProcStructCommands.setVisible(true);
        pnlProcessStructure.updateUI();
    }
    
    private void expandAll(JTree tree, boolean expand) {
        TreeNode root = (TreeNode)tree.getModel().getRoot();
        expandAll(tree, new TreePath(root), expand);
    }
    
    private void expandAll(JTree tree, TreePath parent, boolean expand) {
        TreeNode node = (TreeNode)parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            TreeNode n;
            Enumeration<TreeNode> e = node.children(); 
            while(e.hasMoreElements()) {
                n = e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                expandAll(tree, path, expand);
            }
        }
        if (expand)
            tree.expandPath(parent);
        else
            tree.collapsePath(parent);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if(obj == btExpand) {
            expandAll(treProcessStructure, true);
        }
        else if(obj == btCollapse) {
            expandAll(treProcessStructure, false);
        }
    }
}